#include "ConfigWindow.h"
#include "UserConfig.h"
#include "GlobalConfig.h"
#include "MsnMessenger.h"
#include "MessengerPanel.h"

#include "Util.h"

int ID_RADIO_BUTTON_ENTER = wxNewId();
int ID_RADIO_BUTTON_SHIFTENTER = wxNewId();

int ID_CHECKBOX_STATUS_AWAY = wxNewId();
int ID_CHECKBOX_HISTORY = wxNewId();
int ID_BUTTON_HISTORY_FOLDER = wxNewId();

int ID_BUTTON_FILE_TRANSFER_FOLDER = wxNewId();

int ID_BUTTON_CONNECTION_DIRECT = wxNewId();
int ID_BUTTON_CONNECTION_HTTP = wxNewId();
int ID_BUTTON_CONNECTION_HTTP_PROXY = wxNewId();

BEGIN_EVENT_TABLE(ConfigWindow, wxPropertySheetDialog)
	EVT_CHECKBOX(ID_CHECKBOX_HISTORY, ConfigWindow::OnCheckBoxHistory)

    EVT_BUTTON(ID_BUTTON_HISTORY_FOLDER, ConfigWindow::OnButtonHistoryFolder)

	EVT_BUTTON(ID_BUTTON_FILE_TRANSFER_FOLDER, ConfigWindow::OnButtonFileTransferFolder)

	EVT_RADIOBUTTON(ID_BUTTON_CONNECTION_DIRECT, ConfigWindow::OnConnectionButton)
	EVT_RADIOBUTTON(ID_BUTTON_CONNECTION_HTTP, ConfigWindow::OnConnectionButton)
	EVT_RADIOBUTTON(ID_BUTTON_CONNECTION_HTTP_PROXY, ConfigWindow::OnConnectionButton)
	

END_EVENT_TABLE()

ConfigWindow::ConfigWindow(MainFrame* frame) : mainFrame(frame)
{
    //SetSheetStyle(wxPROPSHEET_TREEBOOK);

	//messengerPanel = mp;

    Create(frame, wxID_ANY, _("Options"));

    CreateButtons(wxOK | wxCANCEL |
                        (int)wxPlatform::IfNot(wxOS_WINDOWS_CE, wxHELP));

    wxBookCtrlBase* notebook = GetBookCtrl();
    notebook->AddPage(CreatePersonalPage(notebook), _("Personal"), true);
//    notebook->AddPage(CreateGeneralPage(notebook), _("General"), false);
    notebook->AddPage(CreateMessagePage(notebook), _("Messages"), false);

	notebook->AddPage(CreateFileTransferPage(notebook), _("File Transfer"), false);

	notebook->AddPage(CreateConnectionPage(notebook), _("Connection"), false);

    LayoutDialog();
}

ConfigWindow::~ConfigWindow()
{

}

void ConfigWindow::LoadSettings()
{
	MsnMessenger* messenger = mainFrame->GetCurrentMessenger();

	if(messenger!=NULL && messenger->IsAlive()){
		UserConfig* userConfig = messenger->GetUserConfig();
	 
		nameTextCtrl->SetValue(userConfig->GetDisplayName());
		messageTextCtrl->SetValue(userConfig->GetPersonalMessage());

		newLineCheckBox->SetValue(userConfig->GetShowNewLine());
		showTimeCheckBox->SetValue(userConfig->GetShowTimeStamp());

		shiftEnterRadioButton->SetValue(userConfig->GetMessageShiftEnter());

		historyCheckBox->SetValue(userConfig->GetKeepHistory());
		historyFolderTextCtrl->SetValue(userConfig->GetHistoryFolder());

		ftFolderTextCtrl->SetValue(userConfig->GetFileTransferFolder());
	}

	int connType = GlobalConfig::GetConnectionType();
	if(connType == 0) directRadioButton->SetValue(true);
	if(connType == 1) httpRadioButton->SetValue(true);
	if(connType == 2) httpProxyRadioButton->SetValue(true);


	httpProxyAddressTextCtrl->SetValue( GlobalConfig::GetHttpProxyAddress());
	if(GlobalConfig::GetHttpProxyPort() != -1){
		httpProxyPortTextCtrl->SetValue(StringUtil::ToString(GlobalConfig::GetHttpProxyPort()));
	}
	httpProxyUsernameTextCtrl->SetValue(GlobalConfig::GetHttpProxyUsername());
	httpProxyPasswordTextCtrl->SetValue(GlobalConfig::GetHttpProxyPassword());
	

	UpdateUIStatus();
}

void ConfigWindow::SaveSettings()
{
	MsnMessenger* messenger = mainFrame->GetCurrentMessenger();

	if(messenger!=NULL && messenger->IsAlive()){
		UserConfig* userConfig = messenger->GetUserConfig();

		//change the name
		if(!userConfig->GetDisplayName().IsSameAs(nameTextCtrl->GetValue())){
			messenger->ChangeName(nameTextCtrl->GetValue());
		}

		userConfig->SetDisplayName(nameTextCtrl->GetValue());
		userConfig->SetPersonalMessage(messageTextCtrl->GetValue());

		userConfig->SetShowNewLine(newLineCheckBox->IsChecked());
		userConfig->SetShowTimeStamp(showTimeCheckBox->IsChecked());

		userConfig->SetMessageShiftEnter(shiftEnterRadioButton->GetValue());

		userConfig->SetKeepHistory(historyCheckBox->IsChecked());
		userConfig->SetHistoryFolder(historyFolderTextCtrl->GetValue());

		userConfig->SetFileTransferFolder(ftFolderTextCtrl->GetValue());
	}

	int connType = 0;
	if(httpRadioButton->GetValue()) connType =1;
	if(httpProxyRadioButton->GetValue()) connType =2;
	
	GlobalConfig::SetConnectionType(connType);

	GlobalConfig::SetHttpProxyAddress(httpProxyAddressTextCtrl->GetValue());
	GlobalConfig::SetHttpProxyPort(StringUtil::ToLong(httpProxyPortTextCtrl->GetValue()));

	GlobalConfig::SetHttpProxyUsername(httpProxyUsernameTextCtrl->GetValue());
	GlobalConfig::SetHttpProxyPassword(httpProxyPasswordTextCtrl->GetValue());

}

void ConfigWindow::UpdateUIStatus()
{

	if(httpProxyRadioButton->GetValue() == true){
		httpProxyAddressTextCtrl->Enable(true);
		httpProxyPortTextCtrl->Enable(true);
		httpProxyUsernameTextCtrl->Enable(true);
		httpProxyPasswordTextCtrl->Enable(true);
	}else{
		httpProxyAddressTextCtrl->Enable(false);
		httpProxyPortTextCtrl->Enable(false);
		httpProxyUsernameTextCtrl->Enable(false);
		httpProxyPasswordTextCtrl->Enable(false);
	}


	//if user signed in
	MsnMessenger* messenger = mainFrame->GetCurrentMessenger();
	if(messenger!=NULL && messenger->IsAlive()){
		nameTextCtrl->Enable(true);

		showTimeCheckBox->Enable(true);
		newLineCheckBox->Enable(true);

		enterRadioButton->Enable(true);
		shiftEnterRadioButton->Enable(true);

		ftFolderTextCtrl->Enable(true);
		ftFolderButton->Enable(true);

		historyCheckBox->Enable(true);
		if(historyCheckBox->IsChecked()){
			historyFolderTextCtrl->Enable(true);
			historyFolderButton->Enable(true);
		}else{
			historyFolderTextCtrl->Enable(false);
			historyFolderButton->Enable(false);
		}
	}else{
		nameTextCtrl->Enable(false);
		
		showTimeCheckBox->Enable(false);
		newLineCheckBox->Enable(false);

		enterRadioButton->Enable(false);
		shiftEnterRadioButton->Enable(false);

		historyCheckBox->Enable(false);
		historyFolderTextCtrl->Enable(false);
		historyFolderButton->Enable(false);

		ftFolderTextCtrl->Enable(false);
		ftFolderButton->Enable(false);
	}

	//not supported
	messageTextCtrl->Enable(false);
	
}

wxPanel* ConfigWindow::CreatePersonalPage(wxWindow* parent)
{
    wxPanel* panel = new wxPanel(parent, wxID_ANY);
    wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );

    wxStaticBoxSizer *nameSizer =
        new wxStaticBoxSizer (new wxStaticBox (panel, wxID_ANY, _("My Display Name")), wxVERTICAL );

    wxStaticText* nameText = new wxStaticText(panel, wxID_ANY, _("Type your name for others to see: "));
    nameTextCtrl = new wxTextCtrl(panel, wxID_ANY, _T(""));
    wxStaticText* messageText = new wxStaticText(panel, wxID_ANY, _("Type your personal message for others to see: "));
    messageTextCtrl = new wxTextCtrl(panel, wxID_ANY, _T(""));
    nameSizer->Add( nameText, 0, wxALL, 5);
    nameSizer->Add( nameTextCtrl, 0, wxALL|wxEXPAND, 5);
    nameSizer->Add( messageText, 0, wxALL, 5);
    nameSizer->Add( messageTextCtrl, 0, wxALL|wxEXPAND, 5);

/*
    wxStaticBoxSizer *pictureSizer =
        new wxStaticBoxSizer (new wxStaticBox (panel, wxID_ANY, _("My picture")), wxHORIZONTAL );
    wxStaticText* showPictureText = new wxStaticText(panel, wxID_ANY, _("Show my display picture "));
    wxButton* showPictureButton = new wxButton(panel, wxID_ANY, _("Change Picture..."));
    pictureSizer->Add( showPictureText, 0, wxALL, 5 );
    pictureSizer->Add( showPictureButton, 0, wxALL, 5 );
*/

    wxStaticBoxSizer *statusSizer = new wxStaticBoxSizer (new wxStaticBox (panel, wxID_ANY, _("My Status")), wxVERTICAL );

    wxCheckBox* awayCheckBox = new wxCheckBox(panel, ID_CHECKBOX_STATUS_AWAY, _("Show me as Away when I'm inactive for "));
    wxTextCtrl* awayTextCtrl = new wxTextCtrl(panel, wxID_ANY, _(""), wxDefaultPosition, wxSize(30, 20));
    wxStaticText* awayText = new wxStaticText(panel, wxID_ANY, _(" minutes"));
    wxBoxSizer *awaySizer = new wxBoxSizer( wxHORIZONTAL );
    awaySizer->Add( awayCheckBox, 0, wxALL, 0 );
    awaySizer->Add( awayTextCtrl, 0, wxALL, 0 );
    awaySizer->Add( awayText, 0, wxALL, 0 );

    statusSizer->Add(awaySizer, 0, 0, 0);

    topSizer->Add(nameSizer, 0, wxALL|wxEXPAND, 5);
//    topSizer->Add(pictureSizer, 0, wxALL|wxEXPAND, 5);
    topSizer->Add(statusSizer, 0, wxALL|wxEXPAND, 5);

    panel->SetSizer(topSizer);
    return panel;
}

wxPanel* ConfigWindow::CreateMessagePage(wxWindow* parent)
{
    wxPanel* panel = new wxPanel(parent, wxID_ANY);

    wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );

	//alert
/*
	wxStaticBoxSizer *alertSizer =
        new wxStaticBoxSizer (new wxStaticBox (panel, wxID_ANY, _T("Alert")), wxVERTICAL );

    wxCheckBox* showBalloonCheckBox = new wxCheckBox(panel, wxID_ANY, _("Show alerts when a new message arrives"));

    alertSizer->Add( showBalloonCheckBox, 0, wxALL, 5 );
*/

    wxStaticBoxSizer *formatSizer =
        new wxStaticBoxSizer (new wxStaticBox (panel, wxID_ANY, _("Message Format")), wxVERTICAL );

	//format
    showTimeCheckBox = new wxCheckBox(panel, wxID_ANY, _("Show time stamp"));
    newLineCheckBox = new wxCheckBox(panel, wxID_ANY, _("Show new line"));

    formatSizer->Add( showTimeCheckBox, 0, wxALL, 5 );
    formatSizer->Add( newLineCheckBox, 0, wxALL, 5 );

	wxStaticBoxSizer *sendSizer =
        new wxStaticBoxSizer (new wxStaticBox (panel, wxID_ANY, _("Send Message")), wxVERTICAL );

	//send message
    enterRadioButton = new wxRadioButton(panel, ID_RADIO_BUTTON_ENTER, _("Press Enter"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
	shiftEnterRadioButton = new wxRadioButton(panel, ID_RADIO_BUTTON_SHIFTENTER, _("Press Shift+Enter"));

    sendSizer->Add( enterRadioButton, 0, wxALL, 5 );
    sendSizer->Add( shiftEnterRadioButton, 0, wxALL, 5 );

	//history
    wxStaticBoxSizer *historySizer =
        new wxStaticBoxSizer (new wxStaticBox (panel, ID_CHECKBOX_HISTORY, _("Message History")), wxVERTICAL );

    historyCheckBox = new wxCheckBox(panel, ID_CHECKBOX_HISTORY, _("Save message history automatically"));
    historySizer->Add( historyCheckBox, 0, wxALL, 5 );

	wxStaticText* historyFolderText = new wxStaticText(panel, wxID_ANY, _("Save message history in this folder:"));

    historyFolderTextCtrl = new wxTextCtrl(panel, wxID_ANY, _T(""), wxDefaultPosition, wxSize(200, 20), wxTE_READONLY );
    historyFolderButton = new wxButton(panel, ID_BUTTON_HISTORY_FOLDER, _("Change..."));

    wxBoxSizer *folderSizer = new wxBoxSizer( wxHORIZONTAL );
    folderSizer->Add( historyFolderTextCtrl, 1, wxALL|wxEXPAND, 0 );
    folderSizer->Add( historyFolderButton, 0, wxLEFT|wxRIGHT, 3 );

	historySizer->Add(historyFolderText, 0, wxALL|wxEXPAND, 5 );
    historySizer->Add(folderSizer, 0, wxALL|wxEXPAND, 5 );

//	topSizer->Add(alertSizer, 0, wxALL|wxEXPAND, 5);
	topSizer->Add(sendSizer, 0, wxALL|wxEXPAND, 5);
    topSizer->Add(formatSizer, 0, wxALL|wxEXPAND, 5);
    topSizer->Add(historySizer, 0, wxALL|wxEXPAND, 5);

    panel->SetSizer(topSizer);
    return panel;
}

wxPanel* ConfigWindow::CreateFileTransferPage(wxWindow* parent)
{
	 wxPanel* panel = new wxPanel(parent, wxID_ANY);
	 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );

	wxStaticText* ftFolderText = new wxStaticText(panel, wxID_ANY, _("Save received file in this folder:"));
	ftFolderTextCtrl = new wxTextCtrl(panel, wxID_ANY, _T(""), wxDefaultPosition, wxSize(200, 20), wxTE_READONLY );
	ftFolderButton = new wxButton(panel, ID_BUTTON_FILE_TRANSFER_FOLDER, _("Change..."));
	
	wxBoxSizer *folderSizer = new wxBoxSizer( wxHORIZONTAL );
    folderSizer->Add( ftFolderTextCtrl, 1, wxALL|wxEXPAND, 0 );
    folderSizer->Add( ftFolderButton, 0, wxLEFT|wxRIGHT, 3 );

	wxStaticBoxSizer *borderSizer =
        new wxStaticBoxSizer (new wxStaticBox (panel, ID_CHECKBOX_HISTORY, _("File Transfer")), wxVERTICAL );	 

	borderSizer->Add(ftFolderText, 0, wxALL|wxEXPAND, 5 );
    borderSizer->Add(folderSizer, 0, wxALL|wxEXPAND, 5 );

	topSizer->Add(borderSizer, 0, wxALL|wxEXPAND, 5);

    panel->SetSizer(topSizer);

    return panel;
}

wxPanel* ConfigWindow::CreateConnectionPage(wxWindow* parent)
{
	wxPanel* panel = new wxPanel(parent, wxID_ANY);
	wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );

	

	directRadioButton = new wxRadioButton(panel, ID_BUTTON_CONNECTION_DIRECT, _("Direct connection"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
	httpRadioButton = new wxRadioButton(panel, ID_BUTTON_CONNECTION_HTTP, _("HTTP connection"));
	httpProxyRadioButton = new wxRadioButton(panel, ID_BUTTON_CONNECTION_HTTP_PROXY, _("HTTP proxy"));



	wxStaticText* proxyAddressText = new wxStaticText(panel, wxID_ANY, _("Address"));
	wxStaticText* proxyPortText = new wxStaticText(panel, wxID_ANY, _("Port"));
	httpProxyAddressTextCtrl = new wxTextCtrl(panel, wxID_ANY, _T(""));
	httpProxyPortTextCtrl = new wxTextCtrl(panel, wxID_ANY, _T(""),wxDefaultPosition, wxSize(40, 25));
	
	wxBoxSizer *proxyServerSizer = new wxBoxSizer( wxHORIZONTAL );
	proxyServerSizer->Add(proxyAddressText, 0, wxALL, 5 );
	proxyServerSizer->Add(httpProxyAddressTextCtrl, 1, wxALL|wxEXPAND, 0 );
	proxyServerSizer->Add(proxyPortText, 0, wxALL, 5 );
	proxyServerSizer->Add(httpProxyPortTextCtrl, 0, wxALL, 0 );


	wxStaticText* proxyUserText = new wxStaticText(panel, wxID_ANY, _("Username"));
	httpProxyUsernameTextCtrl = new wxTextCtrl(panel, wxID_ANY, _T(""));

	wxStaticText* proxyPasswordText = new wxStaticText(panel, wxID_ANY, _("Password"));
	httpProxyPasswordTextCtrl = new wxTextCtrl(panel, wxID_ANY, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD);
	

	wxBoxSizer *proxyUserSizer = new wxBoxSizer( wxHORIZONTAL );
	proxyUserSizer->Add(proxyUserText, 0, wxALL, 5 );
	proxyUserSizer->Add(httpProxyUsernameTextCtrl, 0, wxALL|wxEXPAND, 0 );

	proxyUserSizer->Add(proxyPasswordText, 0, wxALL, 5 );
	proxyUserSizer->Add(httpProxyPasswordTextCtrl, 0, wxALL, 0 );

	wxStaticBoxSizer *borderSizer =
		 new wxStaticBoxSizer (new wxStaticBox (panel, ID_CHECKBOX_HISTORY, _("Connection")), wxVERTICAL );	

	borderSizer->Add(directRadioButton, 0, wxALL|wxEXPAND, 5 );
    borderSizer->Add(httpRadioButton, 0, wxALL|wxEXPAND, 5 );
	borderSizer->Add(httpProxyRadioButton, 0, wxALL|wxEXPAND, 5 );
	borderSizer->Add(proxyServerSizer, 0, wxALL|wxEXPAND, 5 );
	borderSizer->Add(proxyUserSizer, 0, wxALL|wxEXPAND, 5 );


	topSizer->Add(borderSizer, 0, wxALL|wxEXPAND, 5);

	panel->SetSizer(topSizer);
    return panel;
}


void ConfigWindow::OnCheckBoxHistory(wxCommandEvent& event)
{
	UpdateUIStatus();
}

void ConfigWindow::OnButtonHistoryFolder(wxCommandEvent& event)
{
	wxDirDialog dirDialog(this, _("Choose a folder"), historyFolderTextCtrl->GetValue());
    if (dirDialog.ShowModal() == wxID_OK){
		historyFolderTextCtrl->SetValue(dirDialog.GetPath());	
    }
}

void ConfigWindow::OnButtonFileTransferFolder(wxCommandEvent& event)
{
	wxDirDialog dirDialog(this, _("Choose a folder"), ftFolderTextCtrl->GetValue());
    if (dirDialog.ShowModal() == wxID_OK){
		ftFolderTextCtrl->SetValue(dirDialog.GetPath());	
    }
}

void ConfigWindow::OnConnectionButton(wxCommandEvent& event)
{
	UpdateUIStatus();
}

